<%
  'just functions
  public function getRole()
    if Session("admin")="online" then
      getRole = "admin"
    else
      getRole = "guest"
    end if
  end function

  public function IIf(expression, truepart, falsepart)
    if expression = true then
      IIf = truepart
    else
      IIf = falsepart
    end if
  end function

  'reserved functions
  public function CurrentUrl()
    dim host
    dim path
    dim params
    dim url

    host = Request.ServerVariables("HTTP_HOST")
    path = Request.ServerVariables("PATH_INFO")
    params = Request.ServerVariables("Query_String")

    if params = "" then
      url = host & path
    else
      url = host & path & "?" & params
    end if
    
    CurrentUrl = url
  end function

  public function GetRefUrl()
    GetRefUrl = Request.ServerVariables("HTTP_REFERER")
  end function

  public function GetVisitIP()
    dim visitorip
    visitorip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    If visitorip = "" Then visitorip = Request.ServerVariables("REMOTE_ADDR")
    GetVisitIP = visitorip
  end function

  public function getVisitorIP()
    '测试可用
    dim strIPAddr
    dim xforwardip
    dim remoteip
    xforwardip = ServerVariables("HTTP_X_FORWARDED_FOR")
    remoteip = ServerVariables("REMOTE_ADDR")
    If xforwardip = "" OR InStr(xforwardip, "unknown") > 0 Then
      strIPAddr = remoteip
    ElseIf InStr(xforwardip, ",") > 0 Then 
      strIPAddr = Mid(xforwardip, 1, InStr(xforwardip, ",")-1) 
    ElseIf InStr(xforwardip, ";") > 0 Then 
      strIPAddr = Mid(xforwardip, 1, InStr(xforwardip, ";")-1) 
    Else 
      strIPAddr = xforwardip
    End If 
    getVisitorIP = Trim(Mid(strIPAddr, 1, 30)) 
  end function

  public function isEmpty(str)
    if str = Empty or str = "" then
      isEmpty = true
    else
      isEmpty = false
    end if
  end function

  public function myisnull(value, replaceby)
      if value = empty or value="" then
          myisnull = replaceby
      else
          myisnull = value
      end if
  end function

  public function setCookie()
    Response.Cookies("APP").Expires= (now() + 700) '设置过期时间(700天)
  end function

  public function filter(Code)
    dim i
    dim strbld
    dim coder
    if isnull(Code) then 
      coder="" 
      exit function
    end if
    for i = 1 to len(Code)
      select case mid(Code,i,1)
        case "<"       
          coder = coder & "&lt;"
        case ">"
          coder = coder & "&gt;"     
        case "&"
          coder = coder & "&amp;"
        case chr(9)
          coder = coder & "&nbsp; &nbsp; "
        case chr(10)
          coder = coder & "<br />"
        case chr(32)
          coder = coder & "&nbsp;"
        case chr(34)
          coder = coder & "&quot;"
        case chr(39)
          coder = coder & "&#39;"
        case else
          coder = coder & mid(Code,i,1)
      end select
      if i mod 10 = 0 then
        strbld = strbld & coder
        coder = ""
      end if
    next
    strbld = strbld & coder
    filter = strbld
  end function

  public function code_filter(code)
    dim i
    dim strbld
    dim coder
    if isnull(Code) then 
      coder="" 
      exit function
    end if
    for i = 1 to len(Code)
      select case mid(Code,i,1)
        case "<"       
          coder = coder & "&lt;"
        case ">"
          coder = coder & "&gt;"     
        case "&"
          coder = coder & "&amp;"
        case chr(9)
          coder = coder & "&nbsp; &nbsp; "
        case chr(32)
          coder = coder & "&nbsp;"
        case chr(34)
          coder = coder & "&quot;"
        case chr(39)
          coder = coder & "&#39;"
        case else
          coder = coder & mid(Code,i,1)
      end select
      if i mod 10 = 0 then
        strbld = strbld & coder
        coder = ""
      end if
    next
    strbld = strbld & coder
    code_filter = strbld
  end function

  public function date_text(adate, matchcode)
    dim year_month_date
    dim hour_minutes
    dim seconds

    year_month_date = Year(adate) & "-" & fixZero(Month(adate), 2) & "-" & fixZero(Day(adate), 2)
    hour_minutes = fixZero(Hour(adate), 2) & ":" & fixZero(Minute(adate), 2)
    seconds = Second(adate)
    select case matchcode
      case "yyyy-MM-dd HH:mm:ss"
        date_text = year_month_date & " " & hour_minutes & ":" & seconds
      case "yyyy-MM-dd HH:mm"
        date_text = year_month_date & " " & hour_minutes
      case default
        date_text = year_month_date & " " & hour_minutes & ":" & seconds
    end select
  end function

  public function fixlength(data, length, prechar)
    fixlength =  data
    if length > 0 then
      if len(data) < length then
        fixlength = String(length - len(data), prechar) & data
      end if
    end if
  end function

  public function fixZero(data, length)
    fixZero = fixlength(data, length, "0")
  end function

  public function form_to_value(field, request_type, value_type)
    dim data
    if request_type = "form" then
      if Request.Form(field).Count > 0 then
        data = Request.Form(field)(1)
      else
        data = vbNullString
      end if
    elseif request_type = "querystring" then
      data = Request.QueryString(field)
    else
      data = vbNullString
    end if
    select case value_type
      case "string"
        if isEmpty(data) = true then
          form_to_value = vbNullString
        else
          form_to_value = CStr(data)
        end if
      case "integer"
        if isEmpty(data) = true then
          form_to_value = 0
        else
          form_to_value = CInt(data)
        end if
      case "decimal"
        if isEmpty(data) = true then
          form_to_value = 0.0
        else
          form_to_value = CDec(data)
        end if
      case "currency"
        if isEmpty(data) = true then
          form_to_value = 0.0
        else
          form_to_value = CCur(data)
        end if
    end select
  end function

  public function myisnumeric(n)
      if n=Empty then
          myisnumeric=false
          exit function
      end if
      myisnumeric=isnumeric(n)
  end function

  public function ServerVariables(variableName)
    ServerVariables = Request.ServerVariables(variableName)
  end function
%>